package cn.kxm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import cn.kxm.pojo.Dict;
import cn.kxm.pojo.DictDetail;
import cn.kxm.service.DictService;

@Controller
@RequestMapping("/dictDetail")
public class DictController {
    
    @Autowired
	private DictService ds;
    
    @RequestMapping("findAll")
	@ResponseBody
	public Object findAll(@RequestParam(value="dictValue",required=false)Integer dictValue,
			@RequestParam(value="dictKey",required=false)String dictKey,
			@RequestParam(value="dictType",required=false)String dictType,
			Integer page,Integer rows){
    	List<Map<String, Object>> list=ds.getDictList(dictValue, dictKey, dictType, (page-1)*rows, rows);
    	int total=ds.getDictCount(dictValue, dictKey, dictType);
    	Map<String,Object> map=new HashMap<String, Object>();
		map.put("total",total);
		map.put("rows",list);
		return JSONArray.toJSONString(map);
    }
    
    @RequestMapping("findalltype")
	@ResponseBody
	public Object findAllType(){
       List<Dict> list=ds.getDicts();
       return JSONArray.toJSONString(list);	 
    }
    
    @RequestMapping("getDictByType")
   	@ResponseBody
   	public Object getDictByType(String dictType){
    	String value=ds.getDictByType(dictType);
    	int _value = (Integer.parseInt(value)+1);
    	if(_value>99){
    		value=""+_value;
    	}else if(_value>10&&_value<=99){
    		value="0"+_value;
    	}else if(_value<10&&_value>=1){
    		value="00"+_value;
    	}
    	return JSONArray.toJSONString(value);
    }
    
    @RequestMapping("addDict")
   	@ResponseBody
   	public Object addDict(Dict dict,String memo,String show,Integer sortNumber){
    	Map<String, Object> map=new HashMap<String, Object>();
    	DictDetail d=new DictDetail();
		d.setModifyUser("超级管理员");
		d.setCorporateName("凯旋科技");
		d.setMemo(memo);
		d.setShow(show);
		d.setSortNumber(sortNumber);
    	if(ds.addAllDict(dict, d)==1){
    		map.put("result","success");
    	}else{
    		map.put("result","error");
    	}
    	return JSONArray.toJSONString(map);
    }
    
    @RequestMapping("updateDictById")
    public String updateDictById(Model model,Integer id){
    	Map<String, Object> dict=ds.getDictById(id);
    	model.addAttribute("dict",dict);
    	return "web/goods/updateDict";
    }
    
    @RequestMapping("showDictById")
    public String showDictById(Model model,Integer id){
    	Map<String, Object> dict=ds.getDictById(id);
    	model.addAttribute("dict",dict);
    	return "web/goods/showDict";
    }
    
    @RequestMapping("updateDict")
   	@ResponseBody
   	public Object updateDict(Dict dict,String memo,String show,Integer sortNumber){
    	Map<String, Object> map=new HashMap<String, Object>();
    	DictDetail d=new DictDetail();
    	d.setDid(dict.getId());
		d.setModifyUser("超级管理员");
		d.setMemo(memo);
		d.setCorporateName("凯旋科技");
		d.setShow(show);
		d.setSortNumber(sortNumber);
    	if(ds.updateAllDict(dict, d)==1){
    		map.put("result","success");
    	}else{
    		map.put("result","error");
    	}
    	return JSONArray.toJSONString(map);
    }
    
    @RequestMapping("checkDictKey")
	@ResponseBody
	 public Object checkDictKey(String key){
    	Dict dict=ds.checkDictKey(key);
    	Map<String,Object> map=new HashMap<String, Object>();
    	if(dict!=null){
    		map.put("result","cunzai");
    	}else{
    		map.put("result","bucunzai");
    	}
    	return JSONArray.toJSONString(map);
    }
    
    @RequestMapping("/delDict")
    @ResponseBody
    public Object delDict(Integer id){
		Map<String,Object> map=new HashMap<String, Object>();
		if(ds.delOne(id)==1){
			map.put("result","success");
		}else{
			map.put("result","error");
		}
    	return JSON.toJSONString(map);
    }
}
